class friut{
  constructor(ctx){
    this.ctx = ctx ;
    this.img = new Image();
    this.img.src = 'images/Basket.jpg';
    //手推车初始坐标数据
    this.init = {
      x:ctx.canvas.width/2-80/2,
      y:ctx.canvas.height -60,
      w:80,
      h:40
    },
    //是否按住了箩筐(状态位)
    this.status = false;
    //
    this.state = false;
    //判断游戏是否结束
    this.isOver = false;
  }
  gameOver(enemy){
    //判断结束：炸弹与箩筐相碰
    //计算炸弹中心坐标
    let sx = enemy.init.x + enemy.init.w/2;
    let sy = enemy.init.y + enemy.init.h/2;
    if(sx>this.init.x && sx<this.init.x+this.init.w && sy>this.init.y && sy<this.init.y + this.init.h){
      //炸弹与箩筐向碰了 
      this.isOver = true;
    }
  }
  draw(){
    //绘制手推车
    this.ctx.drawImage(this.img,this.init.x,this.init.y,this.init.w,this.init.h);
  }
  //监听箩筐的移动操作
  listen(){
    this.ctx.canvas.addEventListener('touchstart', (e) => {
      //判断是否按住了飞机
      //获取按下时手指的坐标
      let dot = e.changedTouches[0];
      //判断范围
      // console.log(dot)
      if (dot.clientX > this.init.x && dot.clientX < this.init.x + this.init.w && dot.clientY > this.init.y && dot.clientY < this.init.y + this.init.h) {
        this.status = true;
      }
    });
    this.ctx.canvas.addEventListener('touchmove', (e) => {
      if (this.status) {
        //获取手指移动时，实时坐标
        let touch = e.changedTouches[0];
        let x = touch.clientX;
        let y = touch.clientY;
        //修改飞机左上角的坐标(保证手指位于飞机的正中心)
        this.init.x = x - this.init.w / 2;
        this.init.y = y - this.init.h / 2;
        //限制飞机移动边界
        //左边界
        if (this.init.x < 0) {
          this.init.x = 0;
        }
        //右边界
        if (this.init.x > this.ctx.canvas.width - this.init.w) {
          this.init.x = this.ctx.canvas.width - this.init.w;
        }
        //上边界
        if (this.init.y < 0) {
          this.init.y = 0;
        }
        //下边界
        if (this.init.y > this.ctx.canvas.height - this.init.h) {
          this.init.y = this.ctx.canvas.height - this.init.h;
        }
      }
    });
    this.ctx.canvas.addEventListener('touchend', (e) => {
      //手指离开屏幕时，重置状态
      this.status = false;
    });
  }
  isCatch(friut) {
    //判断水果是否被接住
    //计算箩筐的中心坐标
    let x = friut.init.x + friut.init.w / 2;
    let y = friut.init.y + friut.init.h / 2;
    if (x > this.init.x && x < this.init.x + this.init.w && y > this.init.y && y < this.init.y + this.init.h) {
      //箩筐接收水果,控制水果消失
      this.state = false;
      //表示水果已经消失
      return true;
    }
  }
}
export default friut;